const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  return `${[year, month, day]}`
}

const formatDate = (date,splice='-',isTime=false) => {
  const year = date.getFullYear()+''
  let month = (date.getMonth() + 1)+''
  if(month.length<2){
    month = '0'+month
  }
  let day = date.getDate()+''
  if(day.length<2){
    day = '0'+day
  }
  let hour = date.getHours()+''
  if(hour.length<2){
    hour = '0'+hour
  }
  let minute = date.getMinutes()+''
  if(minute.length<2){
    minute = '0'+minute
  }
  let second = date.getSeconds()+''
  if(second.length<2){
    second = '0'+second
  }

  if(isTime){
    return `${year}${splice}${month}${splice}${day} ${hour}:${minute}:${second}`
  }
  return `${year}${splice}${month}${splice}${day}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  formatDate
}
